<?php defined('BASEPATH') OR exit('No direct script access allowed');

class repair extends CI_Controller {

	function __construct()
	{
		parent::__construct();
		if (!$this->ion_auth->logged_in())
		{
			//redirect them to the login page
			redirect('auth/login', 'refresh');
		}
		$this->load->helper(array('form', 'url'));
       	$this->load->model('repair_model');
         $this->load->library('datatables');	
        $this->load->library('upload');		 
	}
	
	
	
	public function new_repair()
	{
$id=$this->session->userdata('user_id');
	$meta['page_title'] = 'New Assigned Repair';
	$this->load->view('common/header',$meta);		
	$this->load->view('repair/new_assigned_repair');
	$this->load->view('common/footer');
	}
	
	
	public function get_new()
	{
	 $id=$this->session->userdata('user_id');
	$at=$this->repair_model->get_view($id);
	print_r($at);
	
	}
	public function get_new_nonwarranty()
	{
	 $id=$this->session->userdata('user_id');
	$at=$this->repair_model->get_view_nonwarranty($id);
	print_r($at);
	
	}
	
	public function view_detail($id)
	{
	
	$this->data['cus']= $this->repair_model->get_cus($id)->row();
	$this->data['case']=$this->repair_model->get_case($id)->row();
	$meta['page_title'] = 'View Detail Repair';
	$this->load->view('common/header',$meta);		
	$this->load->view('repair/new_repair_detail',$this->data);
	$this->load->view('common/footer');
	
	}
	public function view_detail_nonwarranty($id)
	{
	
	$this->data['cus']= $this->repair_model->get_cus($id)->row();
	$this->data['case']=$this->repair_model->get_case_nonwarranty($id)->row();
	$meta['page_title'] = 'View Detail Repair';
	$this->load->view('common/header',$meta);		
	$this->load->view('nonwarranty/new_repair_detail',$this->data);
	$this->load->view('common/footer');
	
	}
	
	public function accept_repair()
	{
	$case_id=$_REQUEST['case_id'];

	$insert=array('status_id'=>6,);
   $this->repair_model->update_repair($case_id,$insert);
   $this->repair_model->update_case_accept($case_id,$insert);
redirect('/engineer/repair/ongoing_process');

}
public function accept_nonwarranty_repair()
	{
	$case_id=$_REQUEST['case_id'];
	$insert=array('status_id'=>7,);
   $this->repair_model->update_nonwarranty_repair($case_id,$insert);
   $this->repair_model->update_nonwarrantycase_accept($case_id,$insert);
redirect('/engineer/repair/ongoing_process');

}
	public function ongoing_process()
  {
  $meta['page_title'] = 'Current Repair';
	$this->load->view('common/header',$meta);		
	$this->load->view('repair/current_repair_view');
	$this->load->view('common/footer');
  
  
  
  
  }
	function getcurrent()
		{ 
	   $id=$this->session->userdata('user_id');
		$at=$this->repair_model->get_current_view($id);
		print_r($at);
		
		}
		function getcurrent_nonwarranty()
		{ 
	   $id=$this->session->userdata('user_id');
		$at=$this->repair_model->get_currentnonwarranty_view($id);
		print_r($at);
		
		}
		
public function ongoing_repair_data($id)
{ 

    $this->datas['case_id']=$id;
	$this->repair_model->update_current_status($id);
	$this->repair_model->update_current_case($id);
    $this->datas['so']=$this->repair_model->get_so($id);
	$this->datas['case']=$this->repair_model->get_docket($id)->row();
    $meta['page_title'] = 'Current Repair Data';
	$this->load->view('common/header',$meta);		
	$this->load->view('repair/current_repair',$this->datas);
	$this->load->view('common/footer');
  


}	
public function ongoing_nonwarranty($id)
{  $this->datas['case_id']=$id;
    $this->datas['part']=$this->repair_model->get_part($id);
	$this->datas['case']=$this->repair_model->get_order($id)->row();
	$this->repair_model->nonwarranty_current_case($id);
	$this->repair_model->nonwarranty_current_status($id);
    $meta['page_title'] = 'Current Repair Data';
	$this->load->view('common/header',$meta);		
	$this->load->view('nonwarranty/current_repair',$this->datas);
	$this->load->view('common/footer');
  
}	
public function defective()
{
if(empty($_REQUEST['chk']))
{
exit();
}
else{
$part=$_REQUEST['chk'];
$case_id=$_REQUEST['case_id'];
$length=count($part);
$reason=$_REQUEST['reason'];
$docket_no=$_REQUEST['docket_no'];
$engineer_id=$this->session->userdata('user_id');
$case_id=$_REQUEST['case_id']; 
$data1['docket_no']=$docket_no;
$data1['case_id']=$case_id;
$data1['engineer_id']=$engineer_id;
$data1['type_id']=$reason;
$id=$this->repair_model->send_defective($data1);
for( $i=0;$i<$length;$i++)
{
$data['so_id']=$part[$i];
$data['defective_id']=$id;

$this->repair_model->send_defective_part($data);

}
$this->repair_model->update_defective_status($case_id);
$this->repair_model->update_defective_case($case_id);

$this->datas['case_id']=$case_id;
$this->datas['case']=$this->repair_model->get_docket($case_id)->row();
$this->datas['defective_so']=$this->repair_model->get_defective_so($case_id);
  $meta['page_title'] = 'Current Repair Data';
  $this->load->view('common/header',$meta);		
  $this->load->view('defective/defective_so',$this->datas);
 $this->load->view('common/footer');

}
}	
public function gathered_detail($id)
{
$meta['page_title'] = 'Detail Of Gathered';
$this->load->view('common/header',$meta);		
$this->load->view('repair/gathered_detail');
$this->load->view('common/footer');
  }
	
public function part_verification($id)
{

$this->data['cus']= $this->repair_model->get_cus($id)->row();
$this->data['case']=$this->repair_model->get_case($id)->row();
$this->repair_model->update_verification($this->data['case']->id);
$this->repair_model->update_case_verification($this->data['case']->id);
$this->data['so']=$this->repair_model->get_so($this->data['case']->id);
$this->repair_model->update_verification($this->data['case']->id);
$this->data['id']=$id;
$meta['page_title'] = 'Dispatch Part Verification';
$this->load->view('common/header',$meta);		
$this->load->view('repair/part_verification',$this->data);
$this->load->view('common/footer');
}	
public function nonwarranty_part_verification($id)
{

$this->data['cus']= $this->repair_model->get_cus($id)->row();
$this->data['case']=$this->repair_model->get_case_nonwarranty($id)->row();
$this->repair_model->update_verification_nonwarranty($this->data['case']->id);
$this->repair_model->update_nonwarranty_case_verification($this->data['case']->id);
$this->data['part']=$this->repair_model->get_part($this->data['case']->id);

$this->repair_model->update_verification($this->data['case']->id);
$this->data['id']=$id;
$meta['page_title'] = 'Dispatch Part Verification';
$this->load->view('common/header',$meta);		
$this->load->view('nonwarranty/part_verification',$this->data);
$this->load->view('common/footer');
}	
public function do_upload()
{
	if (isset($_POST['completed']))
		{ 
$config['upload_path'] = './uploads/';
		$config['allowed_types'] = 'gif|jpg|png|jpeg|doc|txt|pdf|zip|rar';
		$config['max_size']	= '20480';
		$config['max_width']  = '1024';
		$config['max_height']  = '768';
		$this->load->library('upload',$config);
		$case_id=$_REQUEST['case_id'];
		$slo_id=$_REQUEST['sl_no'];
		$so_id=$_REQUEST['so_id'];
		$total=count($slo_id);
		$total=$total+1;
		
		
		for($i=1;$i<$total;$i++)
		{
		$new='new_img';
		$old='old_img';
		$new_img =  $_FILES[$new]['name'];
		$old_img=  $_FILES[$old]['name'];
		$image_old=count($old_img[$i]);
		
		$image=count($new_img[$i]);
		$new_img =  $_FILES[$new]['name'];
		$old_img =  $_FILES[$old]['name'];
		$tmpName  = $_FILES['new_img']['tmp_name'];
		$tname = $_FILES['old_img']['tmp_name'];
	
		for($j=0;$j<$image;$j++)
		{
		 $new_image=$new_img[$i][$j];
		 if(!empty($new_image)){
		$this->repair_model->insert_new($new_image,$so_id[$i-1],$case_id);
			$data =array( 'upload_data' => move_uploaded_file($tmpName[$i][$j],"./uploads/$new_image"));		
			}}
			
			
				for($j=0;$j<$image_old;$j++){
		            $old_image=$old_img[$i][$j];
						 if(!empty($old_image)){
					$this->repair_model->insert($old_image,$so_id[$i-1],$case_id);
					$data =array( 'upload_data' => move_uploaded_file($tname[$i][$j],"./uploads/$old_image"));		
				}}
				}
				$this->repair_model->update_upload_status($case_id);
				$this->repair_model->update_case_upload($case_id);
				$travel=$_REQUEST['travel_expense'];
				
				$fooding=$_REQUEST['fooding_expense'];
				$accomodation=$_REQUEST['accomodation_expense'];
				$conveyance=$_REQUEST['conveyance_expense'];
				$other=$_REQUEST['other_expense'];
				$total=intval($travel)+intval($fooding)+intval($accomodation)+intval($conveyance)+intval($other);
				$expense=array('travel'=>$travel,
				                'case_id'=>$case_id,
								'fooding'=>$fooding,
								'accomodation'=>$accomodation,
								'conveyance'=>$conveyance,
								'other'=>$other,
								'total'=>$total,);
				$this->repair_model->insert_expense($expense);				
              redirect('engineer/repair/completed_repair');
				
				}
				else
				{ if (isset($_POST['new_case']))
				{ 
			    
			    $case_id=$_REQUEST['case_id'];
				$service=$this->repair_model->get_service_id($case_id)->row();
				$service=$service->service_id;
				$max_case['id']=$this->repair_model->get_caseid()->row();
				$max_case= $max_case['id']->id+1;
	            $date=date('M');
			    $date=$date.'/';
		        $newcase=strtoupper('CASE/'.$date.$max_case);
				$insert=array('service_id'=>$service,
				              'case_no'=>$newcase,
							  'status_id'=>0,
							  'engineer_id'=>0,);
							  
				$data=$this->repair_model->create_newcase($insert);
			    $data1=array('old_caseid'=>$case_id,
				             'new_caseid'=>$data,
							 'date'=>date('Y/m/d'));
				
				$this->repair_model->reassigned_case($data1);
				  redirect('engineer/repair/completed_repair');
				}
				}
				
		}
public function completed_repair()
  { 
     $meta['page_title'] = 'ALL Completed Repair';
	$this->load->view('common/header',$meta);		
	$this->load->view('repair/all_repair_view');
	$this->load->view('common/footer');
  
  }
function getall()
		{ 
	   $id=$this->session->userdata('user_id');
		$at=$this->repair_model->get_all_view($id);
		print_r($at);
		
		}
function nonwarranty_getall()
		{ 
	   $id=$this->session->userdata('user_id');
		$at=$this->repair_model->nonwarranty_getall($id);
		print_r($at);
		
		}
public function view_detail_completed($id)
{

	$this->data['cus']= $this->repair_model->get_cus($id)->row();
	$this->data['case']=$this->repair_model->get_case($id)->row();
	$this->data['repair']=$this->repair_model->get_repair_detail($this->data['case']->id)->row();
	$this->data['so']=$this->repair_model->get_so($this->data['case']->id);
	//$abc=$this->repair_model->count_old($this->data['so']->id);
	$this->data['expense']=$this->repair_model->get_expense_detail($this->data['case']->id)->row();
	
	$meta['page_title'] = 'View Detail Repair';
	$this->load->view('common/header',$meta);		
	$this->load->view('repair/detail_completed',$this->data);
	$this->load->view('common/footer');
}
public function enter_expenses($id)
{
$meta['page_title'] = 'Expense';
$this->data['case']=$this->repair_model->get_case($id)->row();
$this->data['expenses']=$this->repair_model->get_expense_detail($this->data['case']->id)->row();
$this->load->view('common/header',$meta);		
$this->load->view('repair/expenses_engineer',$this->data);
$this->load->view('common/footer');
}	
public function completed_nonwarranty()
{
$case_id=$_REQUEST['case_id'];
$report=$_REQUEST['report'];
$this->repair_model->update_repair_final($case_id,$report);
$this->repair_model->update_case_final($case_id);
$exp=$_REQUEST['expense'];
if($exp ==1)
{
$travel=$_REQUEST['travel_expense'];
$fooding=$_REQUEST['fooding_expense'];
$accomodation=$_REQUEST['accomodation_expense'];
$conveyance=$_REQUEST['conveyance_expense'];
$other=$_REQUEST['other_expense'];
$total=intval($travel)+intval($fooding)+intval($accomodation)+intval($conveyance)+intval($other);
				$expense=array('travel'=>$travel,
				                'case_id'=>$case_id,
								'fooding'=>$fooding,
								'accomodation'=>$accomodation,
								'conveyance'=>$conveyance,
								'other'=>$other,
								'total'=>$total,);
								
				$this->repair_model->insert_expense_nonwarranty($expense);

}
else{  } 
	
redirect('engineer/repair/completed_repair');
}	
public function nonwarranty_completed($id)
{
$this->data['cus']= $this->repair_model->get_cus($id)->row();
$this->data['case']=$this->repair_model->get_case_nonwarranty($id)->row();
$this->data['repair']=$this->repair_model->nonwarranty_repair($this->data['case']->id)->row();
$this->data['part']=$this->repair_model->get_part($this->data['case']->id);
$this->data['expense']=$this->repair_model->get_nonwarranty_expense_detail($this->data['case']->id)->row();
$meta['page_title'] = 'View Completed Repair';
$this->load->view('common/header',$meta);		
$this->load->view('nonwarranty/repair_detail',$this->data);
$this->load->view('common/footer');

}
public function defective_so()
{
$case_id=$_REQUEST['case_id'];
$case_no=$_REQUEST['case_no'];
//$docket_no=$_REQUEST['docket_no'];
$sl_no=$_REQUEST['sl_no'];
$length=count($sl_no);
$so_id=$_REQUEST['so_id'];

$old_so=$_REQUEST['old_so'];
$new_so=$_REQUEST['new_so'];
for( $i=0;$i<$length;$i++)
{
$data['so_id']=$so_id[$i];
$data['case_id']=$case_id;
$data['so_no']=$new_so[$i];
$this->repair_model->redispatch_so($data);

} 
$this->repair_model->delete_repair($case_id);
$this->repair_model->update_statuscase($case_id);
                                                     //delete record from repair table and update case status
redirect('engineer/repair/completed_repair');

}
public function enter_expenses_engineer()
{
$case_id=$_REQUEST['case_id'];
$insert=array('case_id'=>$case_id,
              'travel'=>$_REQUEST['travel_expense'],
			   'fooding'=>$_REQUEST['fooding_expense'],
			    'accomodation'=>$_REQUEST['accomodation_expense'],
				 'conveyance'=>$_REQUEST['conveyance_expense'],
                  'other'=>$_REQUEST['other_expense'],
				  'total' =>intval($_REQUEST['travel_expense'])+intval($_REQUEST['fooding_expense'])+intval($_REQUEST['accomodation_expense'])+intval($_REQUEST['conveyance_expense'])+intval($_REQUEST['other_expense']));
$this->repair_model->update_expenses_engineer($insert,$case_id);
redirect('engineer/repair/completed_repair');
}

	
	
	}
	?>